//
//  LeVBOManager.cpp
//  Leste
//
//  Created by 王乐 on 2022/1/26.
//

#include "LeVBOManager.h"
#include <glad/glad.h>

std::map<const char*, unsigned int> LeVBOManager::m_mapVBOs;

LeVBOManager::LeVBOManager() {
}

LeVBOManager::~LeVBOManager() {
}

bool LeVBOManager::findVBO(const char *name, unsigned int &result)
{
    auto iter = m_mapVBOs.find(name);
    if(iter != m_mapVBOs.end())
    {
        result = iter->second;
        return true;
    }
    return false;
}

void LeVBOManager::addVBO(const char* name, float vertices[], unsigned int size)
{
    unsigned int VBO;
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
//    glBindBuffer(GL_ARRAY_BUFFER, 0); // Unbind VBO
    
    m_mapVBOs[name] = VBO;
}

void LeVBOManager::removeVBO(const char *name) { 
    auto iter = m_mapVBOs.find(name);
    if(iter != m_mapVBOs.end())
    {
        unsigned int VBO = iter->second;
        glad_glDeleteBuffers(1, &VBO);
    }
}

